package it.sr.android.smslauncher;

import java.util.concurrent.TimeUnit;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private static boolean esegui = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final Button sendButton = (Button) findViewById(R.id.buttonSend);
		sendButton.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		final EditText phoneNumberEditText = (EditText) findViewById(R.id.editTextPhoneNumber);
		final EditText numberOfSMSEditText = (EditText) findViewById(R.id.editTextNumberOfSMS);

		int numSMS = Integer.valueOf(numberOfSMSEditText.getText().toString());
		long sleep = 2000;
		if (numSMS < 10) {
			int i = 0;
			for (; i < numSMS; i++) {
				if (!esegui)
					break;
				SmsManager.getDefault().sendTextMessage(phoneNumberEditText.getText().toString(), null, "leccamilepalle " + i, null, null);
				try {
					Thread.sleep(sleep);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}

			Toast.makeText(this, "Sent " + i + " SMS.", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(this, "superato il numero massimo di sms consentiti", Toast.LENGTH_LONG).show();
		}
	}

}
